﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using GauntletFramework.Input;

namespace GauntletFramework.StateManagment
{
    public enum GameOrientation
    {
        Horizontal, Vertical, Both
    }

    public struct TimedEvent
    {
        public TimedEvent(TimerEvent Event, float Time)
        {
            this.Event = Event;
            this.Time = Time;
        }

        public delegate bool TimerEvent();//bool determines if event get added to list again
        public TimerEvent Event;
        public float Time;
    }

    public class State : DrawableGameComponent
    {
        public State()
            : base((Game)StaticGame.Game)
        {
            ChildList = new List<DrawableGameComponent>();
            
            RunTime = 0f;
            Initialize();
        }
        public virtual bool StartTransition { get { return true; } }
        public virtual bool EndTransition { get { return true; } }
        public virtual PhoneInputState DefualtPhoneInputState { get { return PhoneInputState.PhoneControlNone; } }
        public virtual GameOrientation GameOrientation { get { return GameOrientation.Both; } }
        public bool Finished = false;

        protected List<DrawableGameComponent> ChildList;
        protected bool UpdateChildren = true;
        protected bool DrawChildren = true;

        public float RunTime;

        protected delegate bool TimerEvent();//bool determines if event get added to list again
        protected IDictionary<string, TimedEvent> TimerList;

        public bool ContentLoaded = false;
        public new virtual void LoadContent()
        {
            base.LoadContent();

            ContentLoaded = true;
        }

        public virtual void Awakened()
        {
        }

        public override void Update(GameTime gameTime)
        {
            RunTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            base.Update(gameTime);

            if (UpdateChildren)
            {
                List<DrawableGameComponent> ChildListClone = new List<DrawableGameComponent>(ChildList);
                foreach (DrawableGameComponent DGC in ChildListClone)
                    DGC.Update(gameTime);
            }

            BasicInput bi = StaticGame.BasicInput;
            if (bi.ButtonBack == AdvancedButtonState.Released)
                Finished = true;
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            if (DrawChildren)
            {
                List<DrawableGameComponent> ChildListClone = new List<DrawableGameComponent>(ChildList);
                foreach (DrawableGameComponent DGC in ChildList)
                    DGC.Draw(gameTime);
            }
        }
    }
}
